package com.ruoyi.statisticalCenter.controller;

import cn.hutool.core.net.URLEncoder;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.cache.CustomCacheFilter;
import com.ruoyi.logistic.domain.PageDate;
import com.ruoyi.logistic.domain.PageInfo;
import com.ruoyi.qualityControl.domain.ScOutBoundSendSheet;
import com.ruoyi.qualityControl.service.ScOutBoundSendSheetService;
import com.ruoyi.statisticalCenter.domain.ScContractAreaSendCostCalculate;
import com.ruoyi.statisticalCenter.domain.ScOutboundSendClientSet;
import com.ruoyi.statisticalCenter.domain.vo.DateStrVo;
import com.ruoyi.statisticalCenter.domain.vo.ScContractAreaSendCostCalculateVo;
import com.ruoyi.statisticalCenter.service.ScContractAreaSendCostCalculateService;
import com.ruoyi.statisticalCenter.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

@RestController
@RequestMapping("/sc/contractAreaSendCostCalculate")
@Api(tags = "统计中心-出港发件成本报表")
public class ScContractAreaSendCostCalculateController {
    @Autowired
    private ScContractAreaSendCostCalculateService scContractAreaSendCostCalculateService;
    @Autowired
    private ScOutBoundSendSheetService scOutBoundSendSheetService;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private RedisUtil redisUtil;

    
    //@CustomCacheFilter
    @ApiOperation("汇总")
    @PostMapping("/collect")
    public R collect(@RequestBody ScContractAreaSendCostCalculateVo vo) {
        if (vo != null && vo.getStartTime() != null && vo.getEndTime() != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        String list = scOutBoundSendSheetService.selectList().toString();
        // 构建Redis键
        String redisKey = "sc_contract_area_send_cost_calculate:collect:" + vo.getStartTime() + ":" + vo.getEndTime()
                + ":" + vo.getCollectType() + list + vo.getPageDate().getPageNum() + vo.getPageDate().getPageSize();
        // 检查Redis中是否存在数据
        /*R redisResult = redisUtil.getObject(redisKey, R.class);
        if (redisResult != null) {
            return redisResult; // 如果Redis中存在数据，则直接返回
        }*/
        // 如果Redis中不存在数据，则从数据库中查询
        R databaseResult = scContractAreaSendCostCalculateService.collect(vo);
        // 将数据库查询结果存储到Redis，并设置过期时间为一天
        redisUtil.setObject(redisKey, databaseResult, 600);
        return databaseResult;
    }

    @ApiOperation("显示承包区配置列表")
    @GetMapping("selectAll")
    public R selectAll(@RequestBody PageDate pageDate) {
        return R.ok(PageInfo.info(scOutBoundSendSheetService.selectAll(pageDate)));
    }

    @ApiOperation("cache")
    @PostMapping("cache")
    public R cache(@RequestBody DateStrVo contractAreaId) {
        return scContractAreaSendCostCalculateService.cache(contractAreaId);
    }

    @ApiOperation("查询承包区配置")
    @GetMapping("/selectSendDisposition/{contractAreaId}")
    public R selectSendDisposition(@PathVariable String contractAreaId) {
        return R.ok(scOutBoundSendSheetService.selectSendDisposition(contractAreaId));
    }

    @ApiOperation("修改承包区配置")
    @PostMapping("/update")
    public R update(@RequestBody ScOutBoundSendSheet scOutBoundSendSheet) {
        Boolean isUpdate = scOutBoundSendSheetService.update(scOutBoundSendSheet);
        if (isUpdate) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(@RequestBody ScContractAreaSendCostCalculateVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
            PageDate pageDate = new PageDate();
            pageDate.setPageNum(1);
            pageDate.setPageSize(1000);
            vo.setPageDate(pageDate);
            if (vo.getCostModel()==null){
                vo.setCostModel(1);
            }
        }
        String fileName = "attachment;filename=" + URLEncoder.createDefault().encode("出港发件成本报表", Charset.forName("utf-8")) + ".xlsx";
        response.setHeader("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//        List<ScContractAreaSendCostCalculate> list = scContractAreaSendCostCalculateService.collectExport(vo);
//        ExcelUtil<ScContractAreaSendCostCalculate> util = new ExcelUtil(ScContractAreaSendCostCalculate.class);
//        util.exportExcel(response, list, "出港发件成本报表");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        scContractAreaSendCostCalculateService.collectExport(vo, outputStream);
    }

    @ApiOperation("客户下拉框")
    @GetMapping("/selectClient")
    public R selectClient() {
        return R.ok(scOutBoundSendSheetService.selectList());
    }

    @ApiOperation("子客户下拉框")
    @GetMapping("/selectChildClient")
    public R selectChildClient() {
        return R.ok(scOutBoundSendSheetService.selectChildList());
    }

    @ApiOperation("根据客户查配置价格")
    @GetMapping("/selectByName")
    public R selectByName(Integer id) {
        return R.ok(scOutBoundSendSheetService.selectByName(id));
    }


    @ApiOperation("修改客户配置")
    @PostMapping("/updateClient")
    public R updateClient(@RequestBody ScOutboundSendClientSet scOutboundSendClientSet) {
        Boolean isUpdate = scOutBoundSendSheetService.updateClientSet(scOutboundSendClientSet);
        if (isUpdate) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

}
